package com.toulezu.test;

import com.ckjava.xutils.*;
import com.toulezu.test.bean.UserBean;
import com.toulezu.test.component.TemplateCom;
import com.toulezu.test.freemarker.Custom;
import com.toulezu.test.freemarker.Subtract;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FreemarkerTestRun implements Constants {

    private static String replaceVariable(String sourceString, Map<String, Object> placeholderMap) {
        if (StringUtils.isNotBlank(sourceString) && sourceString.contains("${") && sourceString.contains("}")) {
            Pattern pattern = Pattern.compile(REG.VARIABLE);
            Matcher matcher = pattern.matcher(sourceString);
            while (matcher.find()) {
                String matcherStr = matcher.group();
                String variable = matcherStr.replaceAll("\\$\\{", "").replaceAll("\\}", "");
                if (placeholderMap != null && !placeholderMap.isEmpty() && placeholderMap.containsKey(variable)) {
                    String variableValue = StringUtils.getStr(placeholderMap.get(variable));
                    sourceString = sourceString.replace("${" + variable + "}", variableValue);
                }
            }
        }
        return sourceString;
    }

    public static void main(String[] args) throws Exception {

        ApplicationContext appc = new AnnotationConfigApplicationContext("com.toulezu.test");

        TemplateCom templateCom = appc.getBean(TemplateCom.class);

        Map<String, Object> resultData = new HashMap<>();
        resultData.put("cus", new Custom());
        resultData.put("va", "a");
        String string = "${cus.add(5, 8)}, ${va}";

        String str = templateCom.renderString(string, resultData);
        System.out.println(str);

        String body = FileUtils.readFileToString(new File("D:\\git-workspace\\play\\SpringBoot\\spring-boot-freemarker\\src\\main\\resources\\test.text"), "utf-8");

        resultData.put("body", body);
        String tempString = "${cus.toJSONString('${body}')}";
        tempString = replaceVariable(tempString, resultData);

        str = templateCom.renderString(tempString, resultData);
        System.out.println(str);
        System.out.println(EncodesUtils.encodeBase64("5aaaaaa\"aaaa'"));



        try {
            resultData = new HashMap<>();
            resultData.put("cus", new Custom());
            resultData.put("sub", new Subtract());

            resultData.put("status", "1");
            resultData.put("finallevel", "B1");

            // 遍历 map
            Map<String, String> dataMap = new HashMap<>();
            dataMap.put("ab", "_ab");
            dataMap.put("ac", "_ac");
            resultData.put("dataMap", dataMap);

            Map<String, UserBean> userMap = new HashMap<>();
            userMap.put("1", new UserBean("1", "rose"));
            userMap.put("2", new UserBean("2", "jack"));
            resultData.put("userMap", userMap);

            // 遍历 list
            List<String> dataList = new ArrayList<>();
            dataList.add("a");
            dataList.add("b");
            dataList.add("c");
            resultData.put("dataList", dataList);

            // 遍历 queue
            LinkedBlockingDeque<String> dataQueue = new LinkedBlockingDeque<>();
            dataQueue.push("a");
            dataQueue.push("b");
            dataQueue.push("c");
            resultData.put("dataQueue", dataQueue);

            // 数字
            resultData.put("int", new Integer(1233312333));
            resultData.put("long", new Long(12333));
            resultData.put("float", new Float(12333.336));
            resultData.put("double", new Double(12333.333));
            resultData.put("BigDecimal", new BigDecimal(12333.335));


            /*resultData.put("nullString", null);
            resultData.put("emptyString", "");*/

            String result = templateCom.renderTemplate("test.ftl", resultData);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
